
function exit(){
	$("#maTypeName").val("");
	$("#operUser").val("");
	$("#typeId").val("");
	load();
	//关闭所有页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit;

	//日期
	laydate.render({
		elem: '#date'
	});

	laydate.render({
		elem: '#time'
	});
	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'edit'){
			var url="../../matype/findById.action";
			var data={"typeId":da.typeId};
			$.post(url,data,function(mes){
				layer.open({
					type: 1	 //此处以iframe举例
					,title: '数据修改'
						,area: ['500px', '260px']
				,offset: 'auto'
					,content:$('#addOrUpdate')
					,success: function(layero){
						$("#operUser").val(mes.oper);
						$("#maTypeName").val(mes.typeName);
						$("#typeId").val(mes.typeId);
						//渲染
						load();
					},end : function() {
						//弹框关闭的回调函数
						$("#addOrUpdate").hide();
						exit();
					}
				});

			},"json");
		}else if(obj.event === 'del'){
			layer.confirm('真的删除行么', function(index){
				var index = layer.load(0, {shade: false});
				var url="../../matype/delete.action";
				var data={"typeId":da.typeId}
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						obj.del();
						layer.close(index);
						layer.msg("删除成功");
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}

	});
	var $ = layui.$, active = {
			reload: function(){
				var date = $('#date');
				var time = $('#time');
				var typeName = $('#typeName');
				//执行重载
				table.reload('idTest', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					date: date.val(),
					time: time.val(),
					typeName: typeName.val(),
				}
				});
			}
	};

	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('.demoTable #insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title: '增加类型'
				,area: ['500px', '260px']
		,offset: 'auto'
			,content:$('#addOrUpdate')
			,end : function() {
				$("#operUser").attr({"disabled":false});
				//弹框关闭的回调函数
				$("#addOrUpdate").hide();
				exit();
			}
		});
	});

	$('#sub').on('click', function(){
		findMaTypeName();
		
		var countyz="";
		$.each($(".clyz"),function(index,item){
			if($(item).val()=="√"){
				countyz++;
			}
		});
		if(countyz==1){
			var index = layer.load(0, {shade: false});
			var url="../../matype/addOrUpdate.action"; 
			var data=$("#myform").serialize();
			$.post(url,data,function(mes){
				layer.close(index);
				if(mes==1){
					exit();
					$("#addOrUpdate").hide();
					parent.layer.msg('操作成功', {
						icon: 1
					});
					//重载表单
					active['reload'] ? active['reload'].call(this) : '';
				}else{
					parent.layer.msg('操作失败', {
						icon: 2
					});
				}
			},"json");
		}
	});

});
function findMaTypeName(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;

		if($("#maTypeName").val()=="" || $("#maTypeName").val()==null ){
			parent.layer.msg('类型名称不能为空', {
				icon: 5,
				anim :6
			});
			$("#maTypeNametyz").val("×");
		}else if($("#maTypeName").val().length>10){
			parent.layer.msg('类型名称长度不能超过10', {
				icon: 5,
				anim :6
			});
			$("#maTypeNametyz").val("×");
		}else{
			$("#maTypeNametyz").val("√");
		}

	});
}

function load() {
	var form = layui.form
	form.render();
}